﻿using Iworld.Author.Model;
using Iworld.Author.Service.Backstage.Export;
using Iworld.basis.Service;
using System;
using System.Linq;
using System.Runtime.Serialization;

namespace Iworld.Author.Service.Backstage.Import
{
    /// <summary>
    /// 获取充值卡记录
    /// </summary>
    [DataContract]
    public class GetRechargeCardRecordsImport : GetPageListCheckAdministratorImportBase
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [DataMember]
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 关键字
        /// </summary>
         [DataMember]
        public string Keyword { set; get; }

        /// <summary>
        /// 是否被使用
        /// </summary>
         [DataMember]
         public bool? IsUsed { set; get; }

        public PageResult<RechargeCardRecordExport> GetRechargeCardRecords(IModelToDbContextOfAuthor db)
        {
            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<RechargeCard> temp = db.RechargeCards.Where(x=>x.AdministratorId == this.Self.Id);
            if (Keyword != null && Keyword.Trim().Length > 0)
            {
                temp = temp.Where(x => x.UsedAuthor.Username.Contains(Keyword)
                    || x.UsedAuthor.Account.Contains(Keyword));
            }
            if (IsUsed!=null)
            {
                temp = temp.Where(x => x.IsUsed ==(bool)IsUsed);
            }
            if (BeginTime != null)
            {
                temp=temp.Where(x => x.UsedTime >= BeginTime);
            }
            if (EndTime != null)
            {
                temp = temp.Where(x => x.UsedTime <= (DateTime)EndTime);
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                return new RechargeCardRecordExport(x);
            });
            return new PageResult<RechargeCardRecordExport>(this.PageIndex, totalCount, pageSize, list);
        }
    }
}
